Data Assimilation and Visualization Short Course

Visualization practical

Wednesday 17th September 2014

Jon Blower

Lesson 1: Brief introduction to Python

Objectives

  1. Learn some of the advantages (and disadvantages) to programming in Python
  2. Learn how to run Python code interactively
  3. Learn how to edit and run Python scripts

(If you are already comfortable with using Python you can probably skip this lesson.)

What is Python?

Python is a free, easy-to-use programming language that can be used for all kinds of things, from writing simple five-line scripts to creating very large pieces of software. It runs on Windows, Mac, Linux and many other systems besides.

Unlike other languages such as Matlab and IDL, Python is not specifically a scientific programming language. Most of the useful scientific functionality comes from software libraries. This is both a good and a bad thing: it is good because the Python community (which is large and growing) is constantly releasing new and useful libraries that we can all use; but the disadvantage is that it can be difficult to find and install the libraries we need, and sometimes scientific users are surprised that common functionality (e.g. data plotting) isn't supported "out of the box".

All the libraries you will need to complete this practical are already installed on the computers we will use. See later in this document for instructions about how you can set up your own Python environment on your own computer.

Running Python interactively

There are two ways in which you can use Python. In interactive mode, you run the Python interpreter and type commands directly into it. The commands are executed as soon as you press Return. Interactive mode is very useful when you are trying new things out.

Run the Python interpreter by going to the Start Menu (bottom left-hand corner of the screen) and selecting All Programs → Python(x,y) → Command Prompts → Python interpreter.

You should see a prompt that looks a bit like this (it will look a bit different on your system):

Python 2.7.6 (default, Mar  7 2014, 20:25:50) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Write a line of Python code, for example "print 2+2" and press Return. You should see the unsurprising result:

>>> print 2+2
4

Getting help in interactive mode

You can get documentation on Python commands and objects with help() and dir(). For example, let's say you wanted to know more about Python's mathematical functions. You can do this:

>>> import math
>>> help(math)

The first line imports the math library, which gives access to common functions such as sin and cos. The second line prints out some documentation for the library (press q to go back to the interactive prompt). You can get documentation on a particular function:

>>> help(math.sin)

and you can get a simple list of functions that are part of the math library with:

>>> dir(math)

Running Python scripts

As you would expect, you can also write your Python commands in a text file (or script) and execute them as a program. You can use any text editor (even Windows' own Notepad) but it's convenient to use a program that is Python-aware.

Using the SciTE text editor

Your machine should already have the SciTE text editor installed. Run this from the Start Menu by selecting All Programs → Python(x,y) → SciTE. Use the File menu to load the script example.py and run it by pressing F5. You should see the output of the script below.

SciTE is probably the easiest way to run simple Python programs and I will assume you will run most scripts in this practical in this way. But there are other methods too.

Running scripts from the command line (optional)

This section is optional - it assumes you know the basics of changing directories and running programs from the command prompt. Don't worry if you don't know this, just skip this section.

You can run Python scripts from the Windows command prompt (the same method works on the Mac or Linux command prompt if you happen to be working on one of these systems). Open the Command Prompt from the Start Menu by selecting Accessories → Command Prompt. Change into a directory containing a Python script using the cd command, then run:

python <scriptname>.py

Spyder: a more sophisticated development environment (optional)

Many programmers use nothing more than a text editor and a command prompt. However, some programmers prefer to use a more sophisticated development environment. Spyder is a Matlab-like development environment that allows you to load and run scripts, get access to documentation and program interactively using a built-in command prompt.

Spyder is provided on the lab PCs, but you don't need to use it unless you prefer this approach (I am not too familiar with it myself so cannot provide much help).

IMPORTANT: Watch out for WHITESPACE!!

Most newcomers to Python are surprised to find that blocks of code are not separated by braces (i.e. "{" and "}" as they are in C, C++, Java and JavaScript), nor are there special commands like "end do" or "end if" as there are in languages like Fortran and Pascal. Instead, Python code blocks are recognised by indentation. A line of code is recognised as being part of the same code block as the previous line if it is indented by the same set of whitespace characters (spaces, tabs etc).

So the following function in Python is fine:

def double(x):
  x = x * 2
  print "the new value of x is", x
  return x

because each line within the function is indented by the same number of spaces (two in this example). But this function would not work:

def double(x):
  x = x * 2
    print "the new value of x is", x
 return x

This may appear strange at first, but it is part of the Python philosophy to make code as human-readable as possible and avoid extraneous characters in scripts.

You can nest indentation too:

def printNumbersDoubled(n):
  for i in range(n):
    i = i * 2
    print i

See the example script example.py for some more examples.

Further reading

The rest of this section contains links and information if you want to find out more. You don't need it for the rest of this practical.

Where to learn more about Python

Using Python as a lab notebook

These notes were created using a great system called IPython Notebook. Although you may be currently looking at a read-only version of these notes, it's possible to run your own notebook server so that you can edit and run the code within the notes themselves. You can do this if you wish. Go to a command prompt and run:

cd /d n:
ipython notebook --no-browser

(The first line changes to the location of the drive containing your files. You may need to replace n: with the name of the drive where your files are stored.) Wait a few seconds and the server will start. Open a web browser (use Chrome or Firefox, not Internet Explorer) at http://localhost:8888 and you will see a directory tree. Navigate to the location of these lecture notes (look for files with an .ipynb extension) and load a notebook. You can then run it "live" - see instructions here: http://ipython.org/notebook.html.

Note that this is not necessary for this practical. But this can be a great way to experiment with Python and the techniques you're learning in this class.


In [ ]: